﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using eStock.Common;
using eStock.SQL;

namespace eStock.DataObjects
{
    public class OrderCollection : DataCollection<Order>
    {
        public override Order GetById(int id)
        {
            return List.Cast<Order>().FirstOrDefault(c => c.Id == id);
        }

        public override void Replace(Order newValue)
        {
            List[IndexOf(GetById(newValue.Id))] = newValue;
        }

        public override bool Update()
        {
            try
            {
                SQLDataReader res;
                foreach (Order c in List)
                {
                    res = MainWindow.Database.QueryRead("SELECT * FROM es_orders WHERE id = " + c.Id);
                    int i = MainWindow.Database.FetchCount(res);
                    if (i == 0)
                    {
                        ((ComponentType)List[List.IndexOf(c)]).Id = Convert.ToInt32(MainWindow.Database.QueryWrite("INSERT INTO es_orders (componentId, projectId, title, amount, status, orderDate, shopId) VALUES " +
                                                                                                     "('" + c.ComponentId + "', '" + c.ProjectId + "', '" + MainWindow.Database.EscapeString(c.Title) + "', '" + c.Amount + "', '" + c.Status + "', '" + c.OrderDate.ToString("yyyy:MM:dd HH:mm:ss") + "', '" + c.ShopId + "');"));
                    }
                    else
                    {
                        MainWindow.Database.QueryWrite("UPDATE es_orders SET componentId = '" + c.ComponentId + "', projectId = '" + c.ProjectId + "', title = '" + MainWindow.Database.EscapeString(c.Title) + "', amount = '" + c.Amount + "', orderDate = '" + c.OrderDate.ToString("yyyy:MM:dd HH:mm:ss") + "', shopId = '" + c.ShopId + "' WHERE id = " + c.Id + ";");
                    }
                }

                res = MainWindow.Database.QueryRead("SELECT * FROM es_orders");
                Dictionary<string, object> data = MainWindow.Database.FetchArray(res);
                List<int> removeIds = new List<int>();
                while (data != null)
                {
                    Order c = GetById(Convert.ToInt32(data["id"]));
                    if (c == null)
                    {
                        removeIds.Add(Convert.ToInt32(data["id"]));
                    }
                    data = MainWindow.Database.FetchArray(res);
                }
                if (removeIds.Count > 0)
                {
                    string sqlString = "DELETE FROM es_orders WHERE ";
                    for (int j = 0; j < removeIds.Count; j++)
                    {
                        if (j != 0)
                            sqlString += " AND ";
                        sqlString += "id = " + removeIds[j];
                    }
                    MainWindow.Database.QueryWrite(sqlString);
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
